/**
 * $Id$ Copyright 2009-2012 Oak Pacific Interactive. All rights reserved.
 */
package com.shuqi.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.shuqi.activity.viewport.CommonTitle;
import com.shuqi.activity.viewport.ToastDialog;
import com.shuqi.application.ShuqiApplication;
import com.shuqi.common.utils.CheckInputUtils;
import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.Util;
import com.shuqi.controller.FindPasswordController;
import com.shuqi.controller.MainController;
import com.shuqi.controller.R;
import com.shuqi.interfaces.EventTypeConstants;
import com.shuqi.interfaces.IEventListener;
import com.shuqi.model.net.listener.HttpRequestErrListener;
import com.shuqi.model.parse.parser.FindPasswordByEmailParser.FindPasswordByEmailParserInfo;
import com.shuqi.statistics.StatisticsEventID;
import com.shuqi.statistics.StatisticsUtils;

/**
 * 
 * @author <a href="mailto:">yaozhen</a>
 * @version 2014-2-11下午4:10:00
 */
public class FindPasswordByEmailActivity extends ActivityBase implements OnClickListener,
        IEventListener, HttpRequestErrListener {

    private EditText mMailEditText;

    private FindPasswordController mController;

    private ToastDialog mLoadingDialog;

    private TextView mMailPointView;
    
    private CommonTitle mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_account_findpasswordbyemail);
        initView();
        mController =
                (FindPasswordController) MainController.getMainManagerInstance().createController(
                        MainController.CONTROLLER_ID_FINDPASSWORD, this);
        mController.addEventListener(this);
    }

    private void initView() {
        mTitle = (CommonTitle) findViewById(R.id.title);
        mTitle.setLeftClickListener(this);
        findViewById(R.id.text_findpwdbymail_send).setOnClickListener(this);
        mMailEditText = (EditText) findViewById(R.id.edit_findpwdbymail);
        mMailPointView = (TextView) findViewById(R.id.mail_point);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left:
                ActivityManager.getInstance().finishActivity(this);
                break;
            case R.id.text_findpwdbymail_send:
                checkEmail();
                break;

            default:
                break;
        }
    }

    private void checkEmail() {
        String email = mMailEditText.getText().toString();
        boolean isEmail = CheckInputUtils.checkEmail(email, mMailPointView);
        if (!isEmail) {
            return;
        } else {
            mMailPointView.setVisibility(View.INVISIBLE);
        }

        StatisticsUtils.onEvent(StatisticsEventID.ACCOUNT_FINDPASSWORD_BYEMAIL_COMPLETE_CLICK);
        Util.hidesoftkeyboard(getApplicationContext(), mMailEditText);
        mController.handle(FindPasswordController.EVENT_TYPE_FINDPWDBYMAIL, email);
        showLoadingDialog(true, false, "正在找回密码");
    }

    private void showLoadingDialog(boolean isLoading, boolean isSuccess, String showText) {
        if (isFinishing()) {
            return;
        }
        if (mLoadingDialog == null) {
            mLoadingDialog = new ToastDialog(this);
            mLoadingDialog.setDissmisOnClickOutSide(false);
        }
        if (isLoading) {
            mLoadingDialog.loading(showText);
            return;
        }
        mLoadingDialog.show(isSuccess, showText);

    }

    private void hideLoadingDialog() {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                if (mLoadingDialog != null) {
                    mLoadingDialog.dismiss();
                }
            }
        };
        ShuqiApplication.getApplicationHandler().post(r);
    }

    @Override
    public void activityLogicForward() {

    }

    @Override
    public void activityInitData() {

    }

    @Override
    public void onEvent(int event, Object data) {
        hideLoadingDialog();
        switch (event) {
            case EventTypeConstants.EVENT_SUCCESS:
                if (data instanceof FindPasswordByEmailParserInfo) {
                    FindPasswordByEmailParserInfo info = (FindPasswordByEmailParserInfo) data;
                    String msg = info.message;
                    if (!TextUtils.isEmpty(msg)) {
                        showMsg(msg);
                    }
                    if (1 == info.code) {
                        // 成功
                        gotoLoginActivity();
                    }
                }

                break;
            default:
                showMsg("网络连接超时");
                break;
        }

    }

    @Override
    public void request_content_io() {

    }

    @Override
    public void request_reponse_io() {

    }

    @Override
    public void request_parse_io() {

    }

    @Override
    public void request_parse_format() {

    }

    @Override
    public void request_net_error() {}

    @Override
    public void request_timeout() {}

    @Override
    public void request_url() {

    }

    private void gotoLoginActivity() {
        LoginActivity.openVerifyForResult(this,LoginActivity.LOGINTYPE_LOGIN);
    }

    @Override
    protected void onPause() {
        super.onPause();
        Util.hidesoftkeyboard(this, mMailEditText);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log4an.v("yz", "---FindPasswordByEmailActivity--onDestroy--");
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
        mController.onDestroy();
    }

    public static void show(Activity context) {
        Intent intent = new Intent(context, FindPasswordByEmailActivity.class);
        ActivityManager.getInstance().startNextActivity(intent, context);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            ActivityManager.getInstance().finishActivity(this);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
